<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        type="info"
        slot="title"
        round
        size="small"
        icon="search"
        class="search-btn"
        to="/search"
        >搜索</van-button
      >
      <!-- 导航栏 -->
    </van-nav-bar>
    <van-tabs
      class="channel-tabs van-tabs__wrap--scrollable"
      v-model="active"
      animated
      swipeable
    >
      <van-tab
        :title="item.name"
        v-for="(item, index) in channels"
        :key="index"
      >
        <article-list :channel="item"></article-list>
      </van-tab>
      <div class="placeholder" slot="nav-right"></div>
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="isChannelEditShow = true"
      >
        <i class="toutiao icon-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 弹层组件 -->
    <van-popup
      v-model="isChannelEditShow"
      position="bottom"
      :style="{ height: '100%' }"
      closeable
      close-icon-position="top-left"
    >
      <channel-edit :myChannels="channels" :active="active" @update-active="onUpdateActive"></channel-edit>
    </van-popup>
  </div>
</template>

<script>
// 导入子组件
import { getUserChannels } from '@/api/user.js'
import ArticleList from '../../components/article-list.vue'
import ChannelEdit from './components/channel-edit.vue'
import { getItem } from '@/utils/storage'
export default {
  name: 'HomePage',
  components: {
    ArticleList,
    ChannelEdit
  },
  data () {
    return {
      active: 0,
      channels: [],
      isChannelEditShow: false
      // 列表内容存储方式1-用一个list存
      // 优点：简单
      // 缺点：没有缓存，用户体验差
      // list: []
      // 列表内容存储方式2-用多个list存储
      // 优点：简单
      // 缺点：不够完美,处理列表能独立,也希望模板,发请求...独立处理,放页面变得简单
      // list0: [],
      // list1: [] .....
    }
  },
  async created () {
    const { data: { data: { channels } } } = await
    getUserChannels()
    console.log()
    if (this.$store.state.user) {
      this.channels = channels
    } else {
      const item = getItem('TOUTIAO_CHANNELS')
      if (item) {
        this.channels = item
      } else {
        this.channels = channels
      }
    }
  },
  mounted () {

  },
  methods: {
    onUpdateActive (index, isChannelEditShow = false) {
      this.active = index
      this.isChannelEditShow = isChannelEditShow
    }
  }
}
</script>

<style lang="less" scoped>
// /deep/影响自定义属性的位置，放到样式的最前面
//scoped它会'当前的组件'的标签添加自定义属性,并且样式最后面添加自定义属性
// .van-nav-bar__title[data-v-5954443c]
// .van-nav-bar__title{
//   max-width: unset !important;
// }

// [data-v-5954443c] .van-nav-bar__title {
// /deep/ 影响自定义属性的位置,放到样式最前面
/deep/ .van-nav-bar__title {
  max-width: unset !important;
}
.search-btn {
  width: 555px;
  height: 64px;
  background-color: #5babfb;
  border: none;
  font-size: 28px;
  .van-icon {
    font-size: 32px;
  }
}
// 标签项
// - 右边框
// - 下边框
// - 宽高
// - 文字大小
// - 文字颜色
// 底部条
// - 宽高
// - 颜色
// - 位置
/deep/ .channel-tabs {
  .van-tab {
    border-right: 1px solid #edeff3;
    min-width: 200px;
    height: 80px;
    .van-tab__text {
      font-size: 27px;
      color: #777777;
    }
  }
  .van-tabs__wrap {
    position: fixed;
    z-index: 1;
    left: 0;
    top: 92px;
    width: 100%;
  }
  .van-tab--active .van-tab__text {
    color: #333 !important;
  }
  .van-tabs__line {
    width: 31px !important;
    height: 6px;
    background: rgba(50, 150, 250, 1);
    border-radius: 3px;
    bottom: 8px;
  }
  .van-tabs__nav {
    padding: 0;
  }
  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;
    &:before {
      content: "";
      position: absolute;
      left: 0;
      width: 1px;
      height: 100%;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
    // i.toutiao {
    //   font-size: 33px;
    // }
  }
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }
  // 列表样式
  .home-container {
    padding-top: 190px;
    padding-bottom: 100px;
  }
}
</style>
